import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'

// 动态路由
const modules = import.meta.glob([
  '../views/first-page/**.vue',
  '../views/my-order/**.vue',
  '../views/shop/**.vue',
  '../views/shop-cart/**.vue'
])

// 定义一个路由数组
const routes: RouteRecordRaw[] = []

for (const key in modules) {
  // 获取对应的路由path
  const name = key.replace('../views/', '').replace('/index.vue', '')
  routes.push({
    path: '/' + name,
    name,
    component: modules[key]
  })
}
// console.log(routes);


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      redirect: '/first-page',
      component: () => import('@/views/index.vue'),
      children: [
        ...routes
      ]
    },
    {
      path: '/experiment',
      name: 'experiment',
      component: () => import('@/views/experiment/index.vue'),
    },
    {
      path: '/search-list',
      name: 'search-list',
      component: () => import('@/views/search-list/index.vue'),
    }
  ]
})

export default router
